(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/***
	 * 桌面右键菜单
	 * @class HteOS.desktop.ContextMenu
	 * @singleton
	 * */
	HteOS.desktop.ContextMenu = {
		
		containerContextMenu:[{
				id :'size',
				icon : 'glyphicon glyphicon-th',
				name :'图标显示',
				items : [{
					id : 'medium',
					name :'大',
					group : 'size',
					handler :function(){
						HteOS.Desktop.setShortcutSize("medium");
					}
				},{
					id : 'small',
					name :'小',
					group : 'size',
					handler :function(){
						HteOS.Desktop.setShortcutSize("small");
					}
				}]
			},{
				id :'fullscreen',
				icon : 'glyphicon glyphicon-fullscreen',
				name :'全屏显示',
				handler :function(){
					var el = document.body;
					var rfs = el.requestFullScreen
							|| el.webkitRequestFullScreen
							|| el.mozRequestFullScreen
							|| el.msRequestFullScreen;
					if (typeof rfs != "undefined" && rfs) {
						rfs.call(el);
					} else if (typeof window.ActiveXObject != "undefined") {
						// for Internet Explorer
						try {
							var wscript = new ActiveXObject("WScript.Shell");
							if (wscript != null) {
								wscript.SendKeys("{F11}");
							}
						} catch (e) {
							HteOS.Notification.show("", "应用提示",
									"无法进行全屏显示，请点击F11键进行全屏显示", true);
						}
					}
				}
			},{
					id :'locker',
					icon : 'glyphicon glyphicon-lock',
					name :'锁定屏幕',
					handler :function(){
						if(HteOS.User.logon === true){
							HteOS.Locker.lock();
						}
					}
				},'-',{
				id :'manager',
				icon : 'glyphicon glyphicon-list',
				name :'应用管理',
				handler :function(){
					HteOS.TaskManager.start({
							id : 'appmanager',
							name : "应用管理",
							path : HteOS.basePath+"tpl/overview.html",
							mode : 'dock'
						});
				}
			},{
					id :'store',
					icon : 'glyphicon glyphicon-folder-close',
					name :'应用市场',
					handler :function(){
						var app = HteOS.AppManager.getByKey("store");
						if (app) {
							HteOS.TaskManager.start(app);
						} else {
							HteOS.Notification.show("", "应用提示",
									"运行[应用市场]失败，请先安装[应用市场]应用");
						}
					}
				},'-',{
				id: 'wallpaper',
				icon: 'glyphicon glyphicon-cog',
				name: '个性化设置',
				handler: function() {
					var app = HteOS.AppManager.getByKey("settings");
					if (app) {
						HteOS.TaskManager.start(app);
					} else {
						HteOS.Notification.show("", "应用提示",
								"运行[个性化设置]，请先安装[个性化设置]应用");
					}
				}
			},{
				id :'profile',
				icon : 'glyphicon glyphicon-user',
				name :'我的HteOS',
				handler :function(){
					var app = HteOS.AppManager.getByKey("myhteos");
					if (app) {
						HteOS.TaskManager.start(app);
					} else {
						HteOS.Notification.show("", "应用提示",
								"运行[个人中心]，请先安装[个人中心]应用");
					}
				}
			},'-',{
					id : 'about',
					name: '关于HteOS',
					icon: 'glyphicon glyphicon-info-sign',
					handler :function(){
						HteOS.Messager.alert("欢迎",HteOS.introduction);
					}
				}],
		
		init : function(){
			this.initShortcutMenu();
			this.initContextMenu();
		},
		
		/**
		 *初始化磁贴的右键菜单
		 */
		initShortcutMenu: function() {
			new HteOS.menu.Menu({
				selector: ".hte-shortcut",
				cls: "hte-shortcut-contextmenu",
				items: [{
					id: 'open',
					icon: 'glyphicon glyphicon-new-window',
					name: '打开',
					handler: function() {
						HteOS.TaskManager.start(this.app);
					}
				},{
					id :'hide',
					icon : 'glyphicon glyphicon-eye-close',
					name :'隐藏',
					handler :function(){
						HteOS.AppManager.hide(this.app);
					}
				},{
					id : 'quick',
					name : "添加到快速启动栏",
					icon : 'glyphicon glyphicon-log-in',
					handler :function(){
						HteOS.AppManager.pink(this.app);
					}
				},'-',{
					id :'uninstall',
					icon : 'glyphicon glyphicon-trash',
					name :'卸载',
					handler :function(){
						HteOS.app.AppManager.uninstall(this.app);
					}
				}],
				events: {
					show: function(target) {
						var me = this,
							shortcut;
						if ($(target).hasClass("hte-shortcut")) {
							shortcut = $(target);
						} else {
							shortcut = $(target).parents(".hte-shortcut");
						}
						shortcut.addClass("selected");
						var app = HteOS.app.AppManager.getByEl(shortcut);
						if (app) {
							if (!app.path) {
								me.disable("open");
								me.disable("quick");
							} else {
								me.enable("open");
								if (app.quick) {
									me.disable("quick");
								} else {
									me.enable("quick");
								}
							}
							if (app.native) {
								me.disable("uninstall");
							} else {
								me.enable("uninstall");
							}
							
							me.app = app;
						} else {
							me.hide();
						}
					},
					hide: function() {
						$(".hte-shortcut.selected").removeClass("selected");
						this.app = null;
					}
				}
			});
		},

		/**
		 * 初始化桌面右键菜单
		 * */
		initContextMenu: function() {
			var selector = (HteOS.UA.isIE8 || HteOS.UA.isIE9) ? ".hte-wallpaper-image" : ".hte-desktop-container";
			new HteOS.menu.Menu({
				selector: selector,
				cls: "hte-context-menu",
				items: this.containerContextMenu,
				events: {
					show : function(){
						var me = this;
						me.group("size").find(".glyphicon").removeClass("glyphicon-ok");
						me.setIcon(HteOS.Desktop.shortcutSize,'glyphicon-ok');
						if(HteOS.User.logon === true){
							this.get("locker").show();
						}else{
							this.get("locker").hide();
						}
					}
				}
			});
		}
	};
}));